package com.niiwoo.civet.trade.service.local.cardniu;

import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.niiwoo.civet.trade.dao.entity.RateConfig;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapperExt;
import com.niiwoo.civet.trade.dao.mapper.RateConfigMapperExt;
import com.niiwoo.civet.trade.dto.response.cardniu.CardniuLoanConfigDTO;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.trade.service.local.configure.TradeConfigLocalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2018/2/26.
 * Description：civet-app
 */
@Service
@Slf4j
public class CardNiuLoanLocalService {

    @Autowired
    private ProjectMapperExt projectMapper;

    @Autowired
    private RateConfigMapperExt rateConfigMapper;

    @Autowired
    private TradeConfigLocalService tradeConfigLocalService;

    /**
     * 获取用户已使用额度
     * @param userId
     * @return
     */
    public BigDecimal getCardNiuUsedCredit(String userId) {
        List<Byte> statusList = new ArrayList<>();
        // 只统计审核中、募集中、还款中的借款金额
        statusList.add(ProjectStatusEnum.CHECKING.getStatus());
        statusList.add(ProjectStatusEnum.COLLECTING.getStatus());
        statusList.add(ProjectStatusEnum.REPAYING.getStatus());
        if (!HintManagerHolder.isMasterRouteOnly()) {
            HintManagerHolder.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.setMasterRouteOnly();
        }
        BigDecimal usedCredit = projectMapper.statisticsLoanAmountByStatus(userId, statusList, null, ProjectTypeEnum.CARD_NIU_LOAN.getValue());
        usedCredit = (usedCredit == null ? BigDecimal.ZERO : usedCredit);
        // 减去已退回标的金额
        List<Integer> subStatusList = new ArrayList<>();
        subStatusList.add(ProjectSubStatusEnum.CHECKING_BACK.getSubStatus());
        subStatusList.add(ProjectSubStatusEnum.CHECKING_BACK_PHONEOPERATOR_TIMEOUT.getSubStatus());
        subStatusList.add(ProjectSubStatusEnum.CHECKING_BACK_ADDRESSLIST_TIMEOUT.getSubStatus());
        if (!HintManagerHolder.isMasterRouteOnly()) {
            HintManagerHolder.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.setMasterRouteOnly();
        }
        BigDecimal backAmount = projectMapper.statisticsLoanAmountByStatus(userId, null, subStatusList, ProjectTypeEnum.CARD_NIU_LOAN.getValue());
        backAmount = (backAmount == null ? BigDecimal.ZERO : backAmount);
        return usedCredit.subtract(backAmount);
    }

    public CardniuLoanConfigDTO getCardniuLoanConfig() {
        CardniuLoanConfigDTO cardniuLoanConfigDTO = new CardniuLoanConfigDTO();

        // 借款费率配置
        List<RateConfig> rateConfigList = rateConfigMapper.getRateConfigByType(ProjectTypeEnum.CARD_NIU_LOAN.getValue());
        List<CardniuLoanConfigDTO.InterestRateInfo> interestRateInfoList = new ArrayList<>();
        for(RateConfig rateConfig : rateConfigList) {
            CardniuLoanConfigDTO.InterestRateInfo interestRateInfo = new CardniuLoanConfigDTO.InterestRateInfo();
            interestRateInfo.setTerm(rateConfig.getDeadline());
            interestRateInfo.setAnualInterestRate(rateConfig.getBorrowInterestRate());
            interestRateInfo.setGuaranteeFeeRate(rateConfig.getGuaranteeServiceFeeRate());
            interestRateInfo.setServiceRate(rateConfig.getPlatformManageFeeRate());
            interestRateInfoList.add(interestRateInfo);
        }
        cardniuLoanConfigDTO.setInterestRateInfo(interestRateInfoList);
        return cardniuLoanConfigDTO;
    }


}
